package com.xty.itgroup.tech05tytex.Logic;

import com.alibaba.fastjson.JSONObject;
import com.xty.itgroup.tech05tytex.Databean.*;
import com.xty.itgroup.tech05tytex.Mapper.Impl.Impl02Customer;
import com.xty.itgroup.tech05tytex.Mapper.Impl.Impl03Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class Logic03Contract {
    @Autowired
    private Impl03Contract contract;
    @Autowired
    private Impl02Customer customer;
    private static Logger log = LoggerFactory.getLogger(Logic03Contract.class);
    @Transactional
    public void addContract(Bean03Contract bc) {
        bc.setTex_contract_order_id(getOrderId(bc.getTex_contract_order_id(), bc.getTex_contract_buyer_id(),0));
        bc.setTex_contract_id(getOrderId(bc.getTex_contract_id(),bc.getTex_contract_supplier_id(),1));
        contract.addContractMain(bc);
        log.info("新合同编号: " + bc.getTex_id());
        contract.addContractDetails(bc.getDetails(), bc.getTex_id());
    }

    @Transactional
    public void modifyContract(Bean03Contract bc) {
        log.info("待修改合同 " + bc.getTex_id());
        Bean03Contract contractMain = contract.getContractMain(bc.getTex_id());
        if (contractMain==null) {
            throw new BeanException(1, "查无此合同");
        }
        if (contractMain.getIs_lock()!=3) {
            throw new BeanException(1, "非草稿状态下无法修改合同");
        }
        //修改主表
        contract.updateContrat(bc);
        //删除子表
        contract.deleteDetailByMain(bc.getTex_id());
        //添加新子表
        if (bc.getDetails()!=null){
            List<Bean03ContractDetail> details = bc.getDetails();
            contract.addContractDetails(details,bc.getTex_id());
        }
    }

    public String getOrderId(String order_id, int customer_id,int flag) {
        Bean02Customer c = customer.getCustomer(customer_id);
        if (c == null) {
            log.info("添加合同 buyerId/saleId： " + customer_id);
            throw new BeanException(1, "无此客户");
        }
        int orderNum = c.getContract_num() + 1;
        log.info("order： " + orderNum);
        customer.updateNum(customer_id);
        return order_id +(flag==1?"-":"")+ orderNum;
    }
    public JSONObject getContract(int tex_id) {
        Bean03Contract bc = contract.getContractMain(tex_id);
        if (bc == null) {
            log.info("错误tex_id: " + tex_id);
            throw new BeanException(1, "查无此合同");
        }
        List<Bean03ContractDetail> bcd = contract.listContractDetail(tex_id);
        Bean02Customer buyer = customer.getCustomer(bc.getTex_contract_buyer_id());
        Bean02CustomerTicket buyer_ticket=customer.getTicketInfo(bc.getTex_buyer_ticket());
        Bean02Customer supplier = customer.getCustomer(bc.getTex_contract_supplier_id());
        Bean02CustomerTicket supplier_ticket=customer.getTicketInfo(bc.getTex_supplier_ticket());
        JSONObject res = new JSONObject();
        res.put("contract", bc);
        res.put("details", bcd);
        res.put("buyer", buyer);
        res.put("buyer_ticket",buyer_ticket);
        res.put("supplier", supplier);
        res.put("supplier_ticket",supplier_ticket);
        return res;
    }
    public List<Map<String, Object>> listContract(String user_name, String customer_name, String stime, String etime, int is_back, int status) {
        return contract.listContractMain(user_name, customer_name, stime, etime, is_back, status);
    }
    public void deleteContract(int tex_id){
        contract.lockContract(tex_id);
    }
    public void statusSet(int tex_id,int status){
        contract.statusSet(tex_id, status);
    }
    public void backDateSet(int tex_id, int status, String contract_back_date) {
        Bean03Contract bc = contract.getContractMain(tex_id);
        if (bc == null) {
            log.info("错误tex_id: " + tex_id);
            throw new BeanException(1, "查无此合同");
        }
        if (bc.getIs_lock() == 3) {
            throw new BeanException(1, "该合同处于草稿状态，无法回签");
        }
        contract.backDateSet(tex_id, status, contract_back_date);
    }
}
